//
//  TabTwoViewController.m
//  DaJiang
//
//  Created by sally on 16/9/24.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "CellManager.h"
#import "FeedbackView.h"
#import "MainModel.h"
#import "TabTwoViewController.h"
#import "UITextView+Placeholder.h"

@interface TabTwoViewController () <UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIView *footerView;
@property (weak, nonatomic) IBOutlet UIButton *sendBtn;
@property (strong, nonatomic) NSMutableArray *tableData;
@property (strong, nonatomic) NSMutableArray *placeholderData;
@property (strong, nonatomic) NSString *companyName;
@property (strong, nonatomic) NSString *userName;
@property (strong, nonatomic) NSString *phone;
@property (strong, nonatomic) NSString *problem;

@end

@implementation TabTwoViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self setNotification];
  [self initControl];
  [self initData];
}

- (void)dealloc {
  [[NSNotificationCenter defaultCenter]removeObserver:self];
}

#pragma mark 设置通知
- (void)setNotification {
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChanged:) name:UITextFieldTextDidChangeNotification object:nil];
}

#pragma mark 初始化控件
- (void)initControl {
  self.title = @"私人定制";
  self.headerView.height = GHeadViewHeight;
  self.sendBtn.layer.borderColor = GOrangeColor.CGColor;
  self.sendBtn.layer.borderWidth = 1;
  self.sendBtn.layer.cornerRadius = GNormalBtnHeight / 2;
  self.sendBtn.layer.masksToBounds = YES;
  [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}

#pragma mark 初始化数据
- (void)initData {
  self.tableData = [NSMutableArray array];
  [self.tableData addObject:@[ @"企业名称", @"联系人", @"联系方式" ]];
  [self.tableData addObject:@[ @"我的需求", @"" ]];
  self.placeholderData = [NSMutableArray array];
  [self.placeholderData addObject:@[ @"请填写企业名称", @"请填写您的真实姓名", @"请填写您的手机号" ]];
  [self.placeholderData addObject:@[ @"", @"请填写您的需求" ]];
  [self initControlText];
}

#pragma mark 初始化控件数据
- (void)initControlText {
  self.companyName = @"";
  self.userName = @"";
  self.phone = @"";
  self.problem = @"";
}

#pragma mark - action
#pragma mark 点击提交
- (IBAction)actionOnSend:(id)sender {
  if (self.companyName.length < 1) {
    [[CommonMethod sharedCommonMethod]showToastInCenter:self.view withMessage:@"请填入企业名称"];
    return;
  }
  if (self.userName.length < 1) {
    [[CommonMethod sharedCommonMethod]showToastInCenter:self.view withMessage:@"请填入联系人"];
    return;
  }
  if (self.phone.length < 1) {
    [[CommonMethod sharedCommonMethod]showToastInCenter:self.view withMessage:@"请填入联系方式"];
    return;
  }
  if (![OperationManager isMobileNumber:self.phone]) {
    [[CommonMethod sharedCommonMethod]showToastInCenter:self.view withMessage:@"请填入正常格式的联系方式"];
    return;
  }
  if (self.problem.length < 1) {
    [[CommonMethod sharedCommonMethod]showToastInCenter:self.view withMessage:@"请填入您的需求"];
    return;
  }
  
  MBProgressHUD *hud = [[CommonMethod sharedCommonMethod]createProgressHUDInView:self.view withMessage:@""];
  [MainModel privateRequestWithCompanyName:self.companyName userName:self.userName phone:self.phone problem:self.problem rewardValue:@"" time:@"" location:@"" locationDesc:@"" completion:^(BOOL isSucceed) {
    if (isSucceed) {
      FeedbackView *feedbackView = [[FeedbackView alloc] initWithFeedbackType:FeedbackType_Customized];
      [feedbackView show];
      //清理编辑框里面数据
      [self initControlText];
      [self.tableView reloadData];
    }else{
      NSError *error = [MainModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }
    hud.hidden = YES;
  }];
  
}

#pragma mark - TableView data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return self.tableData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return [[self.tableData objectAtIndex:section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  return 0.1; //section头部高度
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  return GNormalMargin; //section底部高度
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  if (indexPath.section == 1 && indexPath.row == 1) { //最后一项
    return GTextViewHeight - NavHeight;
  }
  return NavHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
  
  //主标题
  [CellManager configCellNormalText:cell];
  cell.textLabel.text = [[self.tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
  
  //列表项点击性
  cell.selectionStyle = UITableViewCellSelectionStyleNone;
  
  //编辑行
  if (indexPath.section == 0) {
    UITextField *textField = [CellManager configCellNormalTextField:cell];
    textField.tag = indexPath.row;
    switch (textField.tag) {
      case 0:
        textField.text = self.companyName;
        break;
      case 1:
        textField.text = self.userName;
        break;
      case 2:
        textField.text = self.phone;
        break;
      default:
        break;
    }
    textField.placeholder = [[self.placeholderData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    [cell.contentView addSubview:textField];
  }
  
  //编辑框
  if (indexPath.section == 1 && indexPath.row == 1) { //最后一项
    UITextView *textView = [CellManager configCellTextView:cell];
    textView.text = self.problem;
    textView.placeholder = [[self.placeholderData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    textView.delegate = self;
    [cell.contentView addSubview:textView];
  }
  
  return cell;
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView{
  self.problem = textView.text;
}

#pragma mark - 私有方法
#pragma mark textfield中的文本发生改变时
- (void)textFieldChanged:(NSNotification *)notification {
  UITextField *textField = (UITextField *)notification.object;
  switch (textField.tag) {
    case 0:
      self.companyName = textField.text;
      break;
    case 1:
      self.userName = textField.text;
      break;
    case 2:
      self.phone = textField.text;
      break;
    default:
      break;
  }
}

@end
